# app/routers/spot_extra_router.py
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
from sqlalchemy.orm import Session
from typing import List
from app.db.database import get_db
from app.schemas.spot_extra_schema import SpotOneSentence, SpotCoverBind
from app.schemas.spot_schema import SpotOut
from app.schemas.common_schema import R
from app.services.spot_extra_service import SpotExtraService

router = APIRouter(prefix="/spots/extra", tags=["景点扩展"])

# 010 Excel 导入
@router.post("/import/excel", response_model=R)
def import_excel(file: UploadFile = File(...), db: Session = Depends(get_db)):
    if file.content_type not in {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-excel"}:
        raise HTTPException(400, "请上传 .xlsx 或 .xls")
    content = file.file.read()
    code, msg = SpotExtraService.import_excel(db, content)
    return R(code=code, message=msg)

# 010 官方库导入
@router.post("/import/official", response_model=R)
def import_official(db: Session = Depends(get_db)):
    code, msg = SpotExtraService.import_official(db)
    return R(code=code, message=msg)

# 011 一句话秒加
@router.post("/onesentence", response_model=R[SpotOut])
def add_onesentence(body: SpotOneSentence, db: Session = Depends(get_db)):
    code, msg, spot = SpotExtraService.add_by_sentence(db, body.sentence)
    if spot:
        return R(code=code, message=msg, data=spot)
    raise HTTPException(status_code=code, detail=msg)

# 012 批量绑定封面（可选）
# @router.post("/bindcovers", response_model=R)
# def bind_covers(mappings: List[SpotCoverBind], db: Session = Depends(get_db)):
#     code, msg = SpotExtraService.bind_covers(db, mappings)
#     return R(code=code, message=msg)